package cn.jsj.hzu.qm.facecloud.ciuser.dao.impl;

import org.springframework.jdbc.core.JdbcTemplate;

import cn.jsj.hzu.qm.facecloud.ciuser.dao.CiciDao;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Cici;

public class CiciDaoImpl implements CiciDao {
	//SELECT id FROM tb_cici WHERE DATEDIFF(ciin,NOW())=0 ORDER BY ciin DESC; 
	
	JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int addCici(Cici cici) {
		String sql = "INSERT INTO tb_cici VALUES(null, ?,?,NOW(), DATE_ADD(DATE_ADD(NOW(),INTERVAL 4 HOUR),INTERVAL 27 MINUTE), ?, ?)";
		try {
			int i = jdbcTemplate.update(sql, new Object[] {
					cici.getLabid(),
					cici.getUserid(),
					cici.getCiinfo(),
					cici.getCiip()
			});
			
			return i;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	
	public int updateCici(int id) {
		String sql = "UPDATE tb_cici SET `ciout`=NOW() WHERE id= ? ";
		
		int i = jdbcTemplate.update(sql, new Object[] {
				id
		});
		
		return i;
	}

	
	public int queryTodayCiId(int userid, int labid) {
		//倒叙查询今天的签到
		String sql = "SELECT `id` FROM tb_cici WHERE DATEDIFF(ciin,NOW())=0 AND userid=? AND labid=? ORDER BY ciin DESC";
		int i;
		
		try {
			i = jdbcTemplate.queryForObject(sql, Integer.class, new Object[] {userid, labid});
			System.out.println("人"+userid+" 在实验室"+labid+" 签到了，在签到表中的id是"+i+".");
			return i;
		} catch (Exception e) {
			//e.printStackTrace();
			return -1;
		}
	}
}
